package org.magi.sort;


import static org.magi.Utils.switchPos;

/**
 * 2022.3.9 冒泡排序 1.0
 * 2022.6.13 冒泡排序 1.1
 */
public class Bubble implements Sort{
    @Override
    public int[] sort(int[] arr) {
        if (arr == null || arr.length <= 1) { return arr; }

        boolean reset = true;
        int len = arr.length - 1;
        while(reset) {
            reset = false;
            for (int i=0; i<len; i++) {
                if (arr[i] > arr[i + 1]) {
                    switchPos(arr, i, i + 1);
                    reset = true;
                }
            }
            len--;
        }

        return arr;
    }
}
